package com.fw.kafka_chapter_1.concumer;

import org.apache.kafka.clients.consumer.*;
import org.apache.kafka.common.TopicPartition;
import org.apache.kafka.common.serialization.StringDeserializer;

import java.time.Duration;
import java.util.*;

/**
 * @Author:yanwei
 * @Date: 2020/8/29 - 17:52
 */

public class ConcumerEqualizer {
    public static void main(String[] args) {
        Properties properties = new Properties();
        // key 序列化器指定
        properties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
        // value 序列化执行
        properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,StringDeserializer.class.getName());
        // 指定组
        properties.put(ConsumerConfig.GROUP_ID_CONFIG,"fw_group");
        // 指定连接
        properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,"192.168.227.129:9092");
        //设置手动提交开启
        properties.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG,Boolean.FALSE);
        //创建接收器
        KafkaConsumer<String,String> consumer = new KafkaConsumer<String, String>(properties);
        Map<TopicPartition, OffsetAndMetadata> currentOffsets = new HashMap<>();
        // 指定 topic
        //在Kafka中，当有新消费者加入或者订阅的topic数发生变化时
        consumer.subscribe(Arrays.asList("fw"), new ConsumerRebalanceListener() {

            // 次方法来决定提交
            @Override
            public void onPartitionsRevoked(Collection<TopicPartition> collection) {
                // 避免重复消费
                consumer.commitSync(currentOffsets);
            }

            @Override
            public void onPartitionsAssigned(Collection<TopicPartition> collection) {

            }
        });

        while (true){
            // 10 毫秒获取一次
            ConsumerRecords<String, String> poll = consumer.poll(Duration.ofMillis(10));
            for (ConsumerRecord<String, String> context : poll) {
                System.err.println(context.value());
                // 异步提交消费位移，
                // 在发生再均衡动作之前可以通过再均衡监听器的 onPartitionsRevoked回调执行commitSync方法同步提交位移。
                currentOffsets.put(new TopicPartition(context.topic(),context.partition()),new OffsetAndMetadata(context.offset() +1));
            }
            consumer.commitSync();
        }
    }
}
